

#import "ViewController.h"

@interface PhoneData : NSObject
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSString *org;
@property (strong, nonatomic) NSString *phone;
@end

@implementation PhoneData

@end

@interface ViewController ()
{
    NSMutableArray<NSMutableArray<PhoneData *> *> *data;
    NSMutableArray<NSString *> *headers;
    BOOL show;
    UISearchBar *searchBar;
    UITableView *_tableView ;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.title = @"学校通讯录";
    _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *fp = [bundle pathForResource:@"联系人" ofType:@"vcf"];
    NSString *vcf = [[NSString alloc] initWithContentsOfFile:fp encoding:NSUTF8StringEncoding error:nil];
    [self parseVCardString:vcf];
}

-(void)parseVCardString:(NSString*)vcardString
{
    NSArray *lines = [vcardString componentsSeparatedByString:@"\n"];
    
    NSMutableArray<PhoneData *> *d = [NSMutableArray new];
    NSMutableSet<NSString *> *set = [NSMutableSet new];
    PhoneData *item;
    for(NSString* line in lines)
    {
        if ([line hasPrefix:@"BEGIN"])
        {
            // NSlog(@"parse start");
            item = [PhoneData new];
        }else if ([line hasPrefix:@"END"])
        {
            // NSlog(@"parse end");
            [d addObject:item];
        }else if ([line hasPrefix:@"N;"])
        {
            // N;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E4=BD=99;=E9=86=B4;;;
            NSArray *upperComponents = [line componentsSeparatedByString:@":"];
            NSArray *components = [[upperComponents objectAtIndex:1] componentsSeparatedByString:@";"];
            
            NSString * lastName = [components objectAtIndex:0];
            NSString * firstName = [components objectAtIndex:1];
            
            lastName = [lastName stringByReplacingOccurrencesOfString:@"=" withString:@"%"];
            firstName = [firstName stringByReplacingOccurrencesOfString:@"=" withString:@"%"];
            
            // NSlog(@"name %@%@",[lastName stringByRemovingPercentEncoding],[firstName stringByRemovingPercentEncoding]);
        }else if ([line hasPrefix:@"EMAIL;"])
        {
            NSArray *components = [line componentsSeparatedByString:@":"];
            NSString *emailAddress = [components objectAtIndex:1];
            NSLog(@"emailAddress %@",emailAddress);
            
        }else if ([line hasPrefix:@"TEL;"])
        {
            NSArray *components = [line componentsSeparatedByString:@":"];
            NSString *phoneNumber = [components objectAtIndex:1];
             NSLog(@"phoneNumber %@",phoneNumber);
            item.phone = [phoneNumber substringToIndex:11];
        }else if ([line hasPrefix:@"FN;"]){
            //FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E4=BD=99=E9=86=B4
            NSArray *upperComponents = [line componentsSeparatedByString:@":"];
            NSString *name = upperComponents[1];
            name = [name stringByReplacingOccurrencesOfString:@"=" withString:@"%"];
            name = [name stringByRemovingPercentEncoding];
            NSLog(@"FN:%@",name);
            item.name = name;
        }else if ([line hasPrefix:@"ORG;"]){
            NSArray *upperComponents = [line componentsSeparatedByString:@":"];
            NSString *name = upperComponents[1];
            name = [name stringByReplacingOccurrencesOfString:@"=" withString:@"%"];
            name = [name stringByRemovingPercentEncoding];
             NSLog(@"ORG:%@",name);
            item.org = name ? : @"未知";
            [set addObject:item.org];
        }
    }

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return data.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return show ? data[section].count : 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *reuse = [self.description stringByAppendingString:@"Reuse"];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
    }
    PhoneData *item = data[indexPath.section][indexPath.row];
    cell.textLabel.text = item.name;
    cell.detailTextLabel.text = item.phone;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
